package com.xiaoming.project.core;

import java.math.BigDecimal;
import java.util.Random;

public class LotteryDraw {

    /**
     * 0 1 2 3 ... 18
     * 0.05 20
     * 总抽奖次数 和 步数取余 等于0重置标识
     * 随机[0,(1÷中将比例)-距离上次抽中后累计没抽到的次数) < 1
     * 标识为false 
     * 
     * @param args
     */
    public static void main(String[] args) {
        Random random = new Random();
        BigDecimal rate = BigDecimal.valueOf(0.02);
        BigDecimal rateNum = BigDecimal.ONE.divide(rate);
        int keyNum = rateNum.multiply(BigDecimal.valueOf(999)).divide(BigDecimal.valueOf(1000)).intValue();

        int notMixSum = 0;
        int totalMix = 0;
        int count49 = 0;
        for (int i = 0; i < 10000; i++) {
            if (isMix(random, rateNum, keyNum, notMixSum)) {
                totalMix++;
                if (notMixSum == rateNum.subtract(BigDecimal.ONE).intValue()) {
                    count49++;
                }
                notMixSum = 0;
            }
            notMixSum++;
        }
        System.out.println("保底次数："+count49);
        System.out.printf("总中将次数%s", totalMix);
    }

    private static boolean isMix(Random random, BigDecimal rateNum, int keyNum, int notMixSum) {
        if (notMixSum < keyNum) {
            notMixSum = 0;
        }
        return random.nextInt(rateNum.multiply(BigDecimal.valueOf(1000))
                                           .subtract(BigDecimal.valueOf(notMixSum)
                                                             .multiply(BigDecimal.valueOf(1000)))
                                           .intValue()) < 1000;
    }

}
